package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.htfyun.ft.R;
import com.htfyun.ft.view.GridSpacingItemDecoration;
import com.htfyun.ft.view.myrecyclerview.OnRecyclerItemClickListener;
import com.htfyun.ft.viewmodel.FTAction;
import com.htfyun.ft.viewmodel.ViewModelFTGot;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class FragmentMain extends FactoryBaseFragment implements View.OnClickListener {

    private final List<FactoryTestItemsEnum> factoryTestItemsEnumList = new ArrayList<>();

    private RecyclerView recyclerViewFactoryTestItem;
    private RecyclerViewAdapterFTItems adapterFTItems;

    private final int SPAN_COUNT = 3;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_main;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        initBtn(R.id.txtReturn);
        initBtn(R.id.txtAutoTest);

        initViewsForFactoryTestItems();
        initFactoryTestItems();

    }

    @Override
    protected void onVisible() {
        super.onVisible();

        if (adapterFTItems != null) {
            adapterFTItems.notifyDataSetChanged();
        }

    }

    @Override
    protected void onInvisible() {
        super.onInvisible();

    }

    private void initBtn(int resId) {
        View view = findViewById(resId);
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    private void initViewsForFactoryTestItems() {

        recyclerViewFactoryTestItem = findViewById(R.id.recyclerViewFactoryTestItem);

        recyclerViewFactoryTestItem.setLayoutManager(new GridLayoutManager(getContext(), SPAN_COUNT));

        recyclerViewFactoryTestItem.addItemDecoration(new GridSpacingItemDecoration(SPAN_COUNT, 10, true));

        adapterFTItems = new RecyclerViewAdapterFTItems(getContext());
        recyclerViewFactoryTestItem.setAdapter(adapterFTItems);

        recyclerViewFactoryTestItem.addOnItemTouchListener(new OnRecyclerItemClickListener(recyclerViewFactoryTestItem) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh, MotionEvent e) {

                if (vh instanceof RecyclerViewAdapterFTItems.NormalViewHolder) {
                    FactoryTestItemsEnum itemsEnum = ((RecyclerViewAdapterFTItems.NormalViewHolder) vh).getItemsEnum();
                    ViewModelFTGot.getViewModel(getActivity()).setClickedItem(itemsEnum);
                }
            }
        });
    }

    private void initFactoryTestItems() {
        factoryTestItemsEnumList.clear();

        for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {
            if (item.getItemFragment() != null && item.getNVRomKey() != null) {
                factoryTestItemsEnumList.add(item);
            }
        }

        adapterFTItems.setItemsEnumList(factoryTestItemsEnumList);
        getViewModelFT().setFactoryTestItemsEnumList(factoryTestItemsEnumList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txtReturn:
                getViewModelFT().setFTAction(FTAction.finishMe);
                break;
            case R.id.txtAutoTest:
                getViewModelFT().setAutoTest(true);
                break;
        }
    }

}
